package com.stork.cms.fragment;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ServiceHandle.Soap.ServiceCall;
import com.stork.cms.R;
import com.stork.cms.activity.Login;

public class ForgetPasswordFragment extends Fragment implements OnClickListener{

	Button SubmitButton;
	EditText ForgetUserName,Emailid;
	String s1="abc", s2="abc@gmail.com",a,b;
	ServiceCall SC=new ServiceCall();
	String Getpassword=null;
	ProgressDialog progreess;
	TextView forgeterrortext;
	String SendUsername=null;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		View view=inflater.inflate(R.layout.forgetpassword, null);	
		SubmitButton=(Button)view.findViewById(R.id.submitbutton);
		ForgetUserName=(EditText)view.findViewById(R.id.fusername);
		Emailid=(EditText)view.findViewById(R.id.emailid);
		Emailid.setVisibility(View.INVISIBLE);
		SubmitButton.setOnClickListener(this);
		return view;
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	
	
		SendUsername=ForgetUserName.getText().toString();
		if(SendUsername.equals(""))
		{
			ForgetUserName.setError("* Enter the user name");
			ForgetUserName.requestFocus();
		}
		else
		{
		 new GetPassword().execute();
		}
	
	}
	private class GetPassword extends AsyncTask<Void, Void, Void>
	{
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			
			progreess=new ProgressDialog(getActivity());
			progreess.setMessage("Please Wait...");
			progreess.setCancelable(false);
			progreess.show();
		}
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if(progreess.isShowing())
			{
				progreess.dismiss();
			}
		}
		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			
			
			Getpassword=SC.Getpassword(SendUsername);
			Log.w("passworddddddd", Getpassword);
			
			if(Getpassword.equals("444"))
			{
getActivity().runOnUiThread(new Runnable(){
					
					@Override
					public void run() {
				Log.i("welcome to cms","NO DATA found");
				ForgetUserName.setText("");
				Toast.makeText(getActivity(), "Invalid user ", Toast.LENGTH_SHORT).show();
				forgeterrortext.setVisibility(View.VISIBLE);
				forgeterrortext.setTextColor(Color.RED);
					}
					});
			}
			else if(Getpassword.equals("404"))
			{
getActivity().runOnUiThread(new Runnable(){
					
					@Override
					public void run() {
				Log.i("welcome to cms","NO DATA found");
				Toast.makeText(getActivity(), "Please Enter valid E-mail ", Toast.LENGTH_SHORT).show();
					}
					});
			}
			else if(Getpassword.equals("000"))
			{
  getActivity().runOnUiThread(new Runnable(){
					
					@Override
					public void run() {
				Log.i("welcome to cms","NO DATA found");
				Toast.makeText(getActivity(), "Not connect the service ", Toast.LENGTH_SHORT).show();
					}
					});
			}
			else 
			{
getActivity().runOnUiThread(new Runnable(){
					
					@Override
					public void run() {
				Log.i("welcome to cms","NO DATA found");
				Toast.makeText(getActivity(), "Please check the mail for Asy  ", Toast.LENGTH_SHORT).show();
				Intent tomainactivity=new Intent(getActivity(),LoginPageFragment.class);
				startActivity(tomainactivity);
					}
					});
			}
			
			
		
			
			return null;
		}
		
}
}
